from sklearn import svm
from sklearn.feature_extraction.text import CountVectorizer
import time
import numpy as np
import argparse
import sys


from custom.LDA_model import GibbsLDA
import util.PATH as PATH
import util.data_helper as data_helper
import custom.data_helper as c_data_helper

T = 600
n_iteration = 300
alpha = 50/T
# alpha = 0.1
beta = 0.1
'''
SVM-LDA实际上就是将来自LDA的doc_topic矩阵作为特征向量来输入SVM进行分类;
所以说, 这个过程中LDA的作用感觉就是特征提取
'''
def svm_main(I, T, n_iteration, train_docs_list, train_label_list, eval_docs_list, eval_label_list):
	'''
	@:parameter name   数据集的name, 只作为生成最终文件的name用 
	@:parameter dataset_path 数据集的路径
	'''

	mLDA = GibbsLDA(T, n_iteration, alpha, beta, 'svm')
	model, tf_vectorizer = mLDA.create_LDA_model(0, train_docs_list, I)

	train_doc_topic = model.doc_topic_

	tf_eval = tf_vectorizer.transform(eval_docs_list)  # tf为训练
	eval_doc_topic = model.transform(tf_eval, max_iter=n_iteration)  # 要求是array类型

	clf = svm.LinearSVC()       # 导入模型
	print('train_doc_topic: {}'.format(train_doc_topic.shape))
	print('train_label_list: {}'.format(np.array(train_label_list)))
	print('eval_doc_topic: {}'.format(eval_doc_topic.shape))
	print('eval_label_list: {}'.format(np.array(eval_label_list).shape))

	print('正在训练窗口{}的SVM模型:'.format(I))
	clf.fit(train_doc_topic, np.array(train_label_list))      # 训练模型


	# 计算acc
	# predictions = clf.predict(eval_doc_topic)
	# mLDA.calculate_acc(eval_label_list, predictions, eval_label_to_bugids)
	# mLDA.calculate_acc(eval_label_list, predictions)

	# print('predictions: {}'.format(predictions))
	# print('eval_label_list: {}'.format(eval_label_list))


	all_probability = clf.decision_function(eval_doc_topic)  # shape=[n_eval_samples, n_labels], label的顺序是?

	# print('all_probability: {}'.format(all_probability))
	# print('clf.classes_: {}'.format(clf.classes_))        #
	# mLDA.calculate_topK(eval_label_list, all_probability, eval_label_to_bugids, 3)
	mLDA.calculate_svm_topK(eval_label_list, all_probability, 5, clf.classes_)    # clf.classes_, shape=[n_labels], 是label名和索引的映射
	mLDA.write_predictions_to_file('窗口{}_{}'.format(I, 'LDASVM'), eval_label_list,all_probability, clf.classes_)


if __name__ == '__main__':
	start_time = time.clock()
	# name = 'Mozilla'
	# path = PATH.two_path_Mozilla

	# parser = argparse.ArgumentParser()
	# parser.add_argument('index', help='指定数据集的索引')
	# args = parser.parse_args()

	# index = int(args.index)
	# index = 1

	name = 'svm'
	path = ""
	# path = paths[index]
	print('数据集:{}'.format(name))
	svm_main(name, path, T, n_iteration)
	print('花费时间: {}'.format(time.clock() - start_time))